package com.arcsoft.commonbase.utils

import android.graphics.Color
import android.os.CountDownTimer
import android.widget.TextView

/**
 * Created by Administrator on 2017/1/22.
 */
class CountDownTimerUtils : CountDownTimer {
    private var mTextView: TextView

    /**
     * @param textView          The TextView
     * @param millisInFuture    The number of millis in the future from the call
     * to [.start] until the countdown is done and [.onFinish]
     * is called.
     * @param countDownInterval The interval along the way to receiver
     * [.onTick] callbacks.
     */
    constructor(textView: TextView, millisInFuture: Long, countDownInterval: Long) : super(
        millisInFuture,
        countDownInterval
    ) {
        mTextView = textView
    }

    constructor(textView: TextView) : super(60000, 1000) { //60s倒计时
        mTextView = textView
    }

    override fun onTick(millisUntilFinished: Long) {
        mTextView.isClickable = false //设置不可点击
        val time = millisUntilFinished / 1000
        if (time < 10) {
            mTextView.text = (millisUntilFinished / 1000).toString() + "s"
        } else {
            mTextView.text = (millisUntilFinished / 1000).toString() + "s" //设置倒计时时间
        }
        mTextView.setTextColor(Color.parseColor("#6E4DF3"))
    }

    override fun onFinish() {
        mTextView.text = "发送验证码"
        mTextView.isClickable = true //重新获得点击
        mTextView.setTextColor(Color.parseColor("#6E4DF3"))
    }
}